package com.mes.cloud.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.BaseEmployeeMyMenuRequest;
import com.mes.cloud.api.dto.request.employee.*;
import com.mes.cloud.api.dto.response.BaseEmployeeDto;
import com.mes.cloud.api.dto.response.employee.BaseEmployeeOrganizationDto;
import com.mes.cloud.api.dto.response.employee.BaseEmployeePositionCustomerDto;
import com.mes.cloud.api.dto.response.menu.MyMenuDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.BaseEmployeeDao;
import com.mes.cloud.dao.SysEmployeeRoleDao;
import com.mes.cloud.dao.SysMenuDao;
import com.mes.cloud.dao.SysOrganizationDao;
import com.mes.cloud.entity.*;
import com.mes.cloud.entity.vo.BaseEmployeeVo;
import com.mes.cloud.entity.vo.employee.BaseEmployeePositionCustomerVo;
import com.mes.cloud.entity.vo.employee.BaseEmployeeTypeVo;
import com.mes.cloud.entity.vo.role.BaseEmployeeOrganizationVo;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 系统用户表
 *
 * @author Mr.zhang
 * @version 2019-07-31 10:42:39
 * @email 407892370@qq.com
 */
@Service
public class BaseEmployeeService {

    private BaseEmployeeDao baseEmployeeDao;

    private IPKeyGenerator iPKeyGenerator;

    private LogService logService;

    private SysEmployeeRoleDao sysEmployeeRoleDao;

    //组织Dao
    private SysOrganizationDao sysOrganizationDao;

    private SysCodeGeneratorService sysCodeGeneratorService;

    private SysPositionOrganizationAuthService sysPositionOrganizationAuthService;

    private static Pattern pattern = Pattern.compile("\r\n|\r|\n|\n\r");

    private SysMenuDao sysMenuDao;

    @Autowired
    public BaseEmployeeService(BaseEmployeeDao baseEmployeeDao, IPKeyGenerator ipKeyGenerator, LogService logService, SysEmployeeRoleDao sysEmployeeRoleDao, SysOrganizationDao sysOrganizationDao, SysCodeGeneratorService sysCodeGeneratorService
            , SysPositionOrganizationAuthService sysPositionOrganizationAuthService, SysMenuDao sysMenuDao) {
        this.baseEmployeeDao = baseEmployeeDao;
        this.iPKeyGenerator = ipKeyGenerator;
        this.logService = logService;
        this.sysEmployeeRoleDao = sysEmployeeRoleDao;
        this.sysOrganizationDao = sysOrganizationDao;
        this.sysCodeGeneratorService = sysCodeGeneratorService;
        this.sysPositionOrganizationAuthService = sysPositionOrganizationAuthService;
        this.sysMenuDao = sysMenuDao;
    }

    /**
     * 描述 员工分页
     *
     * @param {pageRequest]
     * @return com.shinho.tntframe.core.common.model.CommPager<com.shinho.crm.base.api.dto.response.BaseEmployeeDto>
     * @author 郑样平
     * @date 2019/8/8 18:12
     */
    public CommPager<BaseEmployeeDto> selectPage(BaseEmployeePageRequest pageRequest) {
        Page<BaseEmployeeDto> page = PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        //查询条件
        List<BaseEmployeeVo> baseEmployeeList = baseEmployeeDao.selectEmaloyeeList(pageRequest);
        List<BaseEmployeeDto> baseEmployeeDtoList = new ArrayList<>();
        for (BaseEmployeeVo baseEmployeeVo : baseEmployeeList) {
            BaseEmployeeDto baseEmployeeDto = new BaseEmployeeDto();
            BeanUtils.copyProperties(baseEmployeeVo, baseEmployeeDto);
            baseEmployeeDtoList.add(baseEmployeeDto);
        }
        CommPager<BaseEmployeeDto> pageResult = new CommPager<>(page, baseEmployeeDtoList);
        return pageResult;
    }

    /**
     * 描述 员工列表
     *
     * @param baseEmployeeRequest
     * @return java.util.List<com.shinho.crm.base.api.dto.response.BaseEmployeeDto>
     * @author 郑样平
     * @date 2019/8/8 18:13
     */
    public List<BaseEmployeeDto> list(BaseEmployeeRequest baseEmployeeRequest) {
        //查询条件
        BaseEmployee selectParam = new BaseEmployee();
        BeanUtils.copyProperties(baseEmployeeRequest, selectParam);
        List<BaseEmployee> baseEmployeeList = baseEmployeeDao.select(selectParam);
        List<BaseEmployeeDto> baseEmployeeDtoList = new ArrayList<>();
        for (BaseEmployee baseEmployee : baseEmployeeList) {
            BaseEmployeeDto baseEmployeeDto = new BaseEmployeeDto();
            BeanUtils.copyProperties(baseEmployee, baseEmployeeDto);
            baseEmployeeDtoList.add(baseEmployeeDto);
        }
        return baseEmployeeDtoList;
    }

    /**
     * 描述 员工添加
     *
     * @param {baseEmployeeRequest]
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author 郑样平
     * @date 2019/8/8 18:13
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult insertSelective(BaseEmployeeRequest baseEmployeeRequest) {
        BaseEmployee baseEmployee = new BaseEmployee();
        if (Constants.EMPLOYEE_TYPE_SHOPPING_GUIDE.equals(baseEmployeeRequest.getType())) {
            baseEmployeeRequest.setEmployeeNo(Constants.EMPLOYEE_MS + sysCodeGeneratorService.getCode(Constants.CODE_GENERATOR_EMPLOYEE));
        }
        baseEmployeeRequest.setId(iPKeyGenerator.getGenerateKey());
        baseEmployeeRequest.setUpdateDateStr(DateUtil.updateDateStr());
        baseEmployeeRequest.setCreateBy(UserUtil.getCreateByName());
        baseEmployeeRequest.setUpdateBy(UserUtil.getUpdateByName());
        baseEmployeeRequest.setCreateByNo(UserUtil.getCreateByNo());
        baseEmployeeRequest.setUpdateByNo(UserUtil.getUpdateByNo());
        baseEmployeeRequest.setDataSources(Constants.SOURCE_CRM);
        BeanUtils.copyProperties(baseEmployeeRequest, baseEmployee);
        int count = baseEmployeeDao.insertSelective(baseEmployee);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
            this.insertLog(baseEmployee, Constants.ADD_EMPLOYEE);
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(), HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 描述 员工修改
     *
     * @param {baseEmployeeRequest}
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author 郑样平
     * @date 2019/8/8 18:14
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult updateSelectiveById(BaseEmployeeRequest baseEmployeeRequest) {
        BaseEmployee baseEmployee = new BaseEmployee();
        BeanUtils.copyProperties(baseEmployeeRequest, baseEmployee);
        baseEmployee.setUpdateDateStr(DateUtil.updateDateStr());
        baseEmployee.setUpdateBy(UserUtil.getUpdateByName());
        baseEmployee.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = baseEmployeeDao.updateByPrimaryKeySelective(baseEmployee);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
            this.insertLog(baseEmployee, Constants.UPDATE_EMPLOYEE);
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(), HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 描述 获取员工详情
     *
     * @param id
     * @return com.shinho.tntframe.core.common.model.JsonResult<com.shinho.crm.base.api.dto.response.BaseEmployeeDto>
     * @author 郑样平
     * @date 2019/8/8 18:15
     */
    public JsonResult<BaseEmployeeDto> get(String id) {
        JsonResult jsonResult = new JsonResult();
        BaseEmployee baseEmployee = baseEmployeeDao.selectByPrimaryKey(id);
        if (baseEmployee == null) {
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }
        if (baseEmployee.getMainOrganizationId() != null && !"".equals(baseEmployee.getMainOrganizationId())) {
            SysOrganization sysOrganization = sysOrganizationDao.selectByPrimaryKey(baseEmployee.getMainOrganizationId());
            if (sysOrganization != null) {
                baseEmployee.setMainOrganizationName(sysOrganization.getName());
            }
        }
        BaseEmployeeDto baseEmployeeDto = new BaseEmployeeDto();
        BeanUtils.copyProperties(baseEmployee, baseEmployeeDto);
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        jsonResult.setData(baseEmployeeDto);
        return jsonResult;
    }

    /**
     * 获取我的菜单
     *
     * @return
     */
    public JsonResult<List<MyMenuDto>> myMenuList(BaseEmployeeMyMenuRequest baseEmployeeMyMenuRequest) {
        JsonResult jsonResult = new JsonResult();
        //获取用户的ID
        String employeeId = UserUtil.getEmployeeId();
        List<SysMenu> sysMenuList = baseEmployeeDao.myMenuList(employeeId,baseEmployeeMyMenuRequest.getPlatform());
        if("padmin".equals(UserUtil.getEmployeeNo())){
            SysMenu sysMenu = new SysMenu();
            sysMenu.setDelFlag("0");
            sysMenu.setEnableFlag("1");
            sysMenu.setPlatform(baseEmployeeMyMenuRequest.getPlatform());
            sysMenuList = sysMenuDao.select(sysMenu);
            sysMenuList.sort(new Comparator<SysMenu>() {
                // 返回值为int类型，大于0表示正序，小于0表示逆序
                @Override
                public int compare(SysMenu o1, SysMenu o2) {
                    //sort相等用id排序
                    if (o2.getSort().intValue() == o1.getSort().intValue()) {
                        return o1.getId().compareTo(o2.getId());
                    } else if (o2.getSort().intValue() > o1.getSort().intValue()) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            });
        }
        List<MyMenuDto> myMenuDtoList = new ArrayList<>();
        MyMenuDto myMenuDto = null;
        for (SysMenu sysMenu : sysMenuList) {
            myMenuDto = new MyMenuDto();
            BeanUtils.copyProperties(sysMenu, myMenuDto);
            myMenuDtoList.add(myMenuDto);
        }
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        jsonResult.setData(myMenuDtoList);
        return jsonResult;
    }


    /**
     * 员工号获取权限url
     *
     * @return
     */
    public JsonResult<Set<String>> getAuthUrlByEmployeeNo() {
        JsonResult jsonResult = new JsonResult();
        String employeeNo = UserUtil.getEmployeeNo();
        //工号获取员工信息
        BaseEmployee baseEmployeeParam = new BaseEmployee();
        baseEmployeeParam.setEmployeeNo(employeeNo);
        baseEmployeeParam.setDelFlag(Constants.DEL_FLAG_NO);
        BaseEmployee baseEmployee = baseEmployeeDao.selectOne(baseEmployeeParam);
        if (baseEmployee == null) {
            jsonResult.setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
            jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode());
            return jsonResult;
        }
        //获取用户菜单列表
        List<SysMenu> sysMenuList = baseEmployeeDao.getMenuListByEmployeeIdForAuth(baseEmployee.getId());
        if("padmin".equals(employeeNo)){
            SysMenu sysMenu = new SysMenu();
            sysMenu.setDelFlag("0");
            sysMenu.setEnableFlag("1");
            sysMenuList = sysMenuDao.select(sysMenu);
        }
        //获取菜单列表对应的auth-url
        Set<String> authUrlSet = getMenuAuthUrl(sysMenuList);
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        jsonResult.setData(authUrlSet);
        return jsonResult;
    }

    /**
     * 获取菜单权限URL,SysMenu实体字段auth_url存放菜单权限，auth_url是，相隔的字符串
     *
     * @return
     */
    private Set<String> getMenuAuthUrl(List<SysMenu> sysMenuList) {
        Set<String> authUrlSet = new HashSet<String>();
        String authUrlTemp = "";
        for (SysMenu sysMenu : sysMenuList) {
            authUrlTemp = sysMenu.getAuthUrl();
            authUrlSet.addAll(strToSet(authUrlTemp));
        }
        return authUrlSet;
    }

    /**
     * authUrl 为，相隔
     * authUrl转换为set
     *
     * @param authUrl
     * @return
     */
    private Set<String> strToSet(String authUrl) {
        Set<String> authUrlSet = new HashSet<String>();
        if (StringUtils.isEmpty(authUrl)) {
            return authUrlSet;
        }
        String[] authUrls = authUrl.split(",");
        for (String str : authUrls) {
            Matcher m = pattern.matcher(str);
            str = m.replaceAll("");
            authUrlSet.add(str);
        }
        return authUrlSet;
    }

    /**
     * 获取员工信息
     *
     * @return
     */
    public JsonResult<BaseEmployeeDto> getByEmployeeNo() {
        JsonResult<BaseEmployeeDto> jsonResult = new JsonResult();
        String employeeNo = UserUtil.getEmployeeNo();
        //工号获取员工信息
        BaseEmployeeTypeVo baseEmployee = baseEmployeeDao.selectEmployee(employeeNo);
        if (baseEmployee == null) {
            jsonResult.setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
            jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode());
            return jsonResult;
        }

        //封装角色IDS
        SysEmployeeRole sysEmployeeRole = new SysEmployeeRole();
        sysEmployeeRole.setEmployeeId(baseEmployee.getId());
        sysEmployeeRole.setDelFlag(Constants.DEL_FLAG_NO);
        BaseEmployeeDto baseEmployeeDto = new BaseEmployeeDto();
        BeanUtils.copyProperties(baseEmployee, baseEmployeeDto);

        List<SysEmployeeRole> select = sysEmployeeRoleDao.select(sysEmployeeRole);
        if (!CollectionUtils.isEmpty(select)) {
            Set<String> sets = new HashSet<String>();
            for (SysEmployeeRole employeeRole : select) {
                sets.add(employeeRole.getRoleId());
            }
            baseEmployeeDto.setRoles(sets);
        }
        //获取员工上级
        String superiorRealName = baseEmployeeDao.getSuperiorRealName(baseEmployee.getMainPositionId());
        baseEmployeeDto.setSuperiorRealName(superiorRealName);
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        jsonResult.setData(baseEmployeeDto);
        return jsonResult;
    }

    /**
     * 描述 添加操作日志
     *
     * @param employeeLog, logMessage
     * @return int
     * @author 郑样平
     * @date 2019/8/8 9:03
     */
    private int insertLog(BaseEmployee employeeLog, String logMessage) {
        SysEmployeeLog sysEmployeeLog = new SysEmployeeLog();
        sysEmployeeLog.setEmployeeId(employeeLog.getId());
        sysEmployeeLog.setMenuCode(Constants.EMPLOYEE_MENU_CODE);
        sysEmployeeLog.setLog(logMessage);
        sysEmployeeLog.setId(iPKeyGenerator.getGenerateKey());
        sysEmployeeLog.setUpdateDateStr(DateUtil.updateDateStr());
        sysEmployeeLog.setCreateBy(UserUtil.getCreateByName());
        sysEmployeeLog.setUpdateBy(UserUtil.getUpdateByName());
        sysEmployeeLog.setCreateByNo(UserUtil.getCreateByNo());
        sysEmployeeLog.setUpdateByNo(UserUtil.getUpdateByNo());
        return logService.insertEmployeeLog(sysEmployeeLog);
    }

    public JsonResult updateAuthorisationType(String id, int authorisationType) {

        BaseEmployee baseEmployee = new BaseEmployee();
        baseEmployee.setId(id);
        baseEmployee.setAuthorisationType(authorisationType);
        int count = baseEmployeeDao.updateByPrimaryKeySelective(baseEmployee);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(), HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 描述 批量离职操作
     *
     * @param {baseEmployeeStatusRequest]
     * @return com.shinho.tntframe.core.common.model.JsonResult
     * @author 郑样平
     * @date 2019/8/10 9:18
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult batchStatus(BaseEmployeeStatusRequest baseEmployeeStatusRequest) {
        JsonResult jsonResult = new JsonResult();
        baseEmployeeStatusRequest.setUpdateDateStr(DateUtil.updateDateStr());
        baseEmployeeStatusRequest.setUpdateBy(UserUtil.getUpdateByName());
        baseEmployeeStatusRequest.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = baseEmployeeDao.batchStatus(baseEmployeeStatusRequest);
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
            for (String id : baseEmployeeStatusRequest.getIds()) {
                BaseEmployee baseEmployee = new BaseEmployee();
                baseEmployee.setId(id);
                this.insertLog(baseEmployee, Constants.BATCH_STATUS);
            }
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(), HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 同步数据-获取员工信息
     *
     * @param synchroEmployeeGetByIdRequest
     * @return
     */
    public JsonResult<BaseEmployeeDto> synchroGetById(SynchroEmployeeGetByIdRequest synchroEmployeeGetByIdRequest) {
        JsonResult jsonResult = new JsonResult();
        BaseEmployee baseEmployeeParam = new BaseEmployee();
        baseEmployeeParam.setId(synchroEmployeeGetByIdRequest.getId());
        baseEmployeeParam.setDelFlag(Constants.DEL_FLAG_NO);
        BaseEmployee baseEmployee = baseEmployeeDao.selectByPrimaryKey(baseEmployeeParam);
        if (baseEmployee == null) {
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }
        BaseEmployeeDto baseEmployeeDto = new BaseEmployeeDto();
        BeanUtils.copyProperties(baseEmployee, baseEmployeeDto);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(baseEmployeeDto);

    }

    /**
     * 根据组织查询员工
     *
     * @param pageRequest
     * @return
     */
    public CommPager<BaseEmployeeOrganizationDto> listPageByOrgId(BaseEmployeeOrganizationPageRequest pageRequest) {
        //如果传入的组织id为空 默认匹配当前登录者所有组织
//        if(StringUtils.isEmpty(pageRequest.getOrganizationId())){
//            JsonResult<List<String>> orgIdResult= sysPositionOrganizationAuthService.getOrganizationIdList();
//            pageRequest.setOrganizationIds(orgIdResult.getData());
//        }
        Page<BaseEmployeeDto> page = PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        pageRequest.setEmployeeId(UserUtil.getEmployeeId());
        //查询条件
        List<BaseEmployeeOrganizationVo> baseEmployeeList = baseEmployeeDao.selectPageByOrganizationId(pageRequest);
        List<BaseEmployeeOrganizationDto> baseEmployeeDtoList = new ArrayList<>();
        for (BaseEmployeeOrganizationVo baseEmployeeVo : baseEmployeeList) {
            BaseEmployeeOrganizationDto baseEmployeeDto = new BaseEmployeeOrganizationDto();
            BeanUtils.copyProperties(baseEmployeeVo, baseEmployeeDto);
            baseEmployeeDtoList.add(baseEmployeeDto);
        }
        CommPager<BaseEmployeeOrganizationDto> pageResult = new CommPager<>(page, baseEmployeeDtoList);
        return pageResult;
    }

    /**
     * <查询岗位下员工>
     *
     * @param pageRequest
     * @return com.shinho.tntframe.core.common.model.CommPager<com.shinho.crm.base.api.dto.response.employee.BaseEmployeePositionCustomerDto>
     * @author xy
     * @date 2019/9/12 15:35
     */
    public CommPager<BaseEmployeePositionCustomerDto> listPageByPositionId(BaseEmployeePositionPageRequest pageRequest) {
        Page<BaseEmployeeDto> page = PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        //查询条件
        List<BaseEmployeePositionCustomerVo> baseEmployeeList = baseEmployeeDao.listPageByPositionId(pageRequest);
        List<BaseEmployeePositionCustomerDto> baseEmployeeDtoList = new ArrayList<>();
        for (BaseEmployeePositionCustomerVo baseEmployeeVo : baseEmployeeList) {
            BaseEmployeePositionCustomerDto baseEmployeeDto = new BaseEmployeePositionCustomerDto();
            BeanUtils.copyProperties(baseEmployeeVo, baseEmployeeDto);
            baseEmployeeDtoList.add(baseEmployeeDto);
        }
        CommPager<BaseEmployeePositionCustomerDto> pageResult = new CommPager<>(page, baseEmployeeDtoList);
        return pageResult;
    }

    /**
     * @Author ysd
     * @Description 同步员工查询
     * @Date 14:40 2019/11/17
     **/
    public JsonResult<List<BaseEmployeeOrganizationDto>> synchroDataList(@Valid @RequestBody List<String> employeeNos) {
        JsonResult<List<BaseEmployeeOrganizationDto>> jsonResult = new JsonResult();
        //查询条件
        List<BaseEmployeeOrganizationVo> resultList = baseEmployeeDao.synchroDataList(employeeNos);
        List<BaseEmployeeOrganizationDto> dtoList = new ArrayList<>();

        for (BaseEmployeeOrganizationVo baseEmployee : resultList) {
            BaseEmployeeOrganizationDto baseEmployeeDto = new BaseEmployeeOrganizationDto();
            BeanUtils.copyProperties(baseEmployee, baseEmployeeDto);
            dtoList.add(baseEmployeeDto);
        }
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(dtoList);
        return jsonResult;
    }

    /**
     * 通过员工岗位关联的岗位类型，岗位类型关联的角色，删除原有的角色，现在岗位类型对应的角色
     *
     * @param baseEmployeeUpdateRoleByPositionTypeRequest
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult updateRoleByPositionType(BaseEmployeeUpdateRoleByPositionTypeRequest baseEmployeeUpdateRoleByPositionTypeRequest) {
        JsonResult jsonResult = new JsonResult();
        for (String employeeId : baseEmployeeUpdateRoleByPositionTypeRequest.getEmployeeIds()) {
            //删除员工已有的角色
            sysEmployeeRoleDao.deleteByEmployee(employeeId, DateUtil.updateDateStr(), UserUtil.getUpdateByNo(), UserUtil.getUpdateByName());
            //获取员工的岗位-岗位类型-角色
            List<String> roleList = baseEmployeeDao.getPositionTypeRole(employeeId);
            if (CollectionUtils.isEmpty(roleList)) {
                continue;
            }
            //绑定员工角色关系
            //组装员工角色对象
            List<SysEmployeeRole> sysEmployeeRoleList = generatorSysEmployeeRole(roleList, employeeId);
            if (!CollectionUtils.isEmpty(sysEmployeeRoleList)) {
                //批量插入
                sysEmployeeRoleDao.batchInsert(sysEmployeeRoleList);
            }
        }
        return jsonResult;
    }

    /**
     * 组装SysEmployeeRole关联数据
     *
     * @param idList     角色ID集合
     * @param employeeId 员工ID
     * @return
     */
    private List<SysEmployeeRole> generatorSysEmployeeRole(List<String> idList, String employeeId) {
        List<SysEmployeeRole> sysEmployeeRoleList = new ArrayList<SysEmployeeRole>();
        if (CollectionUtils.isEmpty(idList)) {
            return sysEmployeeRoleList;
        }
        SysEmployeeRole sysEmployeeRoleTemp = null;
        for (String id : idList) {
            sysEmployeeRoleTemp = generatorSysEmployeeRole(employeeId, id);
            sysEmployeeRoleList.add(sysEmployeeRoleTemp);
        }
        return sysEmployeeRoleList;
    }

    /**
     * 组织一个SysEmployeeRole
     *
     * @param employeeId
     * @param roleId
     * @return
     */
    private SysEmployeeRole generatorSysEmployeeRole(String employeeId, String roleId) {
        SysEmployeeRole sysEmployeeRoleTemp = new SysEmployeeRole();
        sysEmployeeRoleTemp.setId(iPKeyGenerator.getGenerateKey());
        sysEmployeeRoleTemp.setEmployeeId(employeeId);
        sysEmployeeRoleTemp.setRoleId(roleId);
        sysEmployeeRoleTemp.setUpdateDateStr(DateUtil.updateDateStr());
        sysEmployeeRoleTemp.setCreateBy(UserUtil.getCreateByName());
        sysEmployeeRoleTemp.setUpdateBy(UserUtil.getUpdateByName());
        sysEmployeeRoleTemp.setCreateByNo(UserUtil.getCreateByNo());
        sysEmployeeRoleTemp.setUpdateByNo(UserUtil.getUpdateByNo());
        return sysEmployeeRoleTemp;
    }

    public JsonResult isExist(String employeeNo) {
        BaseEmployee entity = new BaseEmployee();
        entity.setEmployeeNo(employeeNo);
        BaseEmployee baseEmployee = baseEmployeeDao.selectOne(entity);
        if (null == baseEmployee
                || Constants.DEL_FLAG_YES.equals(baseEmployee.getDelFlag())
                || Constants.ENABLE_FLAG_NO.equals(baseEmployee.getEnableFlag())
                || Constants.ALLOW_LOGIN_NO.equals(baseEmployee.getAllowLogin())) {
            return JsonResult.ok().setData(Boolean.FALSE);
        }
        return JsonResult.ok().setData(Boolean.TRUE);
    }

    public JsonResult<List<MyMenuDto>> myMenuListMenu(BaseEmployeeMyMenuRequest baseEmployeeMyMenuRequest) {
        JsonResult jsonResult = new JsonResult();
        //获取用户的ID
        String employeeId = UserUtil.getEmployeeId();
        List<SysMenu> sysMenuList = baseEmployeeDao.myMenuListMenu(employeeId,baseEmployeeMyMenuRequest.getPlatform());
        if("padmin".equals(UserUtil.getEmployeeNo())){
            SysMenu sysMenu = new SysMenu();
            sysMenu.setDelFlag("0");
            sysMenu.setEnableFlag("1");
            sysMenuList = sysMenuDao.select(sysMenu);
            sysMenuList.sort(new Comparator<SysMenu>() {
                // 返回值为int类型，大于0表示正序，小于0表示逆序
                @Override
                public int compare(SysMenu o1, SysMenu o2) {
                    //sort相等用id排序
                    if (o2.getSort().intValue() == o1.getSort().intValue()) {
                        return o1.getId().compareTo(o2.getId());
                    } else if (o2.getSort().intValue() > o1.getSort().intValue()) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            });
        }
        List<MyMenuDto> myMenuDtoList = new ArrayList<>();
        MyMenuDto myMenuDto = null;
        for (SysMenu sysMenu : sysMenuList) {
            myMenuDto = new MyMenuDto();
            BeanUtils.copyProperties(sysMenu, myMenuDto);
            myMenuDtoList.add(myMenuDto);
        }
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        jsonResult.setData(myMenuDtoList);
        return jsonResult;
    }
}
